﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Net;

namespace 按键时序
{
    class SocketClient
    {
        //TcpClient tcpClient;
        //string Host;
        //int Port;

        //bool isConnected;

        //public SocketClient(string ipAddress, int port)
        //{
        //    isConnected = false;

        //    Host = ipAddress;
        //    Port = port;

        //    tcpClient = new TcpClient();
        //    tcpClient.Connect(Host, Port);

        //    if (tcpClient != null)
        //    {
        //        Console.WriteLine("连接服务器成功");
        //        isConnected = true;
        //        NetworkStream networkStream = tcpClient.GetStream();
        //        BinaryReader br = new BinaryReader(networkStream);
        //        BinaryWriter bw = new BinaryWriter(networkStream);
        //        bw.Write("connect succeed...\r\n");  //向服务器发送字符串  
        //        while (true)
        //        {
        //            try
        //            {
        //                string brString = br.ReadString();     //接收服务器发送的数据  
        //                if (brString != null)
        //                {
        //                    Console.WriteLine("接收到服务器发送的数据{0}", brString);
        //                }
        //            }
        //            catch
        //            {
        //                break;        //接收过程中如果出现异常，将推出循环  
        //            }
        //        }
        //    }

        //    isConnected = false;

        //    Console.WriteLine("连接服务器失败");
        //}

        Socket client;
        Thread t1;
        Thread t2;
        bool isLife = false;//标记是否连接到服务器
        string Host;
        int Port;
        public SocketClient(string ipAddress,int port)
        {
            Host = ipAddress;
            Port = port;
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建客户端
            client.Connect(new IPEndPoint(IPAddress.Parse(Host), Port));//将客户端连接到服务器

            if (client != null)
            {
                Console.WriteLine("连接成功");

                isLife = true;
                t1 = new Thread(reciveMsg);
                t1.Start();//开启线程接收消息

                t2 = new Thread(sendMsg);
                t2.Start();//开启线程发送消息
            }



            //Console.ReadKey();
        }

        void reciveMsg()
        {
            while (isLife)
            {
                byte[] buffer = new byte[1024];
                try
                {
                    int length = client.Receive(buffer);
                    string res = Encoding.UTF8.GetString(buffer, 0, length);
                    Console.WriteLine("接收到的消息：" + res);
                }
                catch (Exception e)
                {
                    Console.WriteLine("reciveMsg:  End");
                    break;
                }

            }

        }

        void sendMsg()
        {
            //while (isLife)
            //{
            //    string s = "test...";
            //    try
            //    {
            //        client.Send(Encoding.UTF8.GetBytes(s));
            //    }
            //    catch (Exception e)
            //    {
            //        Console.WriteLine("sendMsg:  End");
            //        break;
            //    }

            //    if (s.Equals("886"))
            //    {
            //        isLife = false;
            //        client.Close();
            //    }
            //}
            int i = 3;
            while (i-->0)
            {
                string s = string.Format("test...{0}",i) ;
                try
                {
                    client.Send(Encoding.UTF8.GetBytes(s));
                }
                catch (Exception e)
                {
                    Console.WriteLine("sendMsg:  End");
                    break;
                }

                if (s.Equals("886"))
                {
                    isLife = false;
                    client.Close();
                }

                Thread.Sleep(1000);
            }
        }

    }
}
